package com.usian.wemedia.controller;

import com.usian.apis.wemedia.WmNewsControllerApi;
import com.usian.model.admin.dtos.NewsAuthDto;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.media.dtos.WmNewsDto;
import com.usian.model.media.dtos.WmNewsPageReqDto;
import com.usian.model.media.pojos.WmNews;
import com.usian.model.media.pojos.WmNewsVo;
import com.usian.wemedia.service.WmNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/news")
public class WmNewsController implements WmNewsControllerApi {

    @Autowired
    private WmNewsService wmNewsService;

    /**
     * 文章列表查询
     *
     * @param dto
     * @return
     */
    @Override
    @PostMapping("/list")
    public ResponseResult findAll(@RequestBody WmNewsPageReqDto dto) {
        return wmNewsService.findAll(dto);
    }

    /**
     * 保存、修改、草稿文章
     *
     * @param wmNews
     * @return
     */
    @Override
    @PostMapping("/submit")
    public ResponseResult submitNews(@RequestBody WmNewsDto wmNews) {
        if (wmNews.getStatus() == WmNews.Status.SUBMIT.getCode()) {
            //提交文章  保存 和修改
            return wmNewsService.submitNews(wmNews, WmNews.Status.SUBMIT.getCode());
        } else {
            //保存草稿  草稿
            return wmNewsService.submitNews(wmNews, WmNews.Status.NORMAL.getCode());
        }
    }

    /**
     * 根据id 查询文章详情
     *
     * @param id
     * @return
     */
    @Override
    @GetMapping("/one/{id}")
    public ResponseResult findWmNewsById(@PathVariable("id") Integer id) {
        return wmNewsService.findWmNewsById(id);
    }


    @Override
    @GetMapping("/findWmNewsFeignById/{id}")
    public WmNews findWmNewsFeignById(@PathVariable Integer id){
        return wmNewsService.getById(id);
    }

    @Override
    @PostMapping("/updateWmNews")
    public boolean updateWmNews(@RequestBody WmNews wmNews){
        boolean b = wmNewsService.updateById(wmNews);
        return b;
    }

    /**
     * 查询审核通过待发布的文章列表
     * @return
     */
    @Override
    @GetMapping("/findRelease")
    public List<Integer> findRelease() {
        return wmNewsService.findRelease();
    }

    /**
     * 分页查询人工审核列表
     *
     * @param dto
     * @return
     */
    @Override
    @PostMapping("/findList")
    public ResponseResult findList(@RequestBody NewsAuthDto dto) {
        return wmNewsService.findList(dto);
    }

    /**
     * 人工审核列表查看详情
     *
     * @param id
     * @return
     */
    @Override
    @GetMapping("/find_news_vo/{id}")
    public WmNewsVo findById(@PathVariable Integer id){
        return wmNewsService.findById(id);
    }

    /**
     * 删除文章
     *
     * @param id
     * @return
     */
    @Override
    @GetMapping("/del_news/{id}")
    public ResponseResult delNews(@PathVariable("id") Integer id) {
        return wmNewsService.delNews(id);
    }

    /**
     * 文章上下架
     *
     * @param dto
     * @return
     */
    @PostMapping("/down_or_up")
    @Override
    public ResponseResult downOrUp(@RequestBody WmNewsDto dto) {
        return wmNewsService.downOrUp(dto);
    }
}